package terra.model.object.action;

import terra.model.object.GameObject;
import terra.model.object.ai.AbstractAI;
import terra.model.object.interfaces.Attacker;

public class ActionFear extends AbstractAction {

	private static final long serialVersionUID = 6728633910541554326L;
	public Attacker attacker;
	public int distanceToEnd = 360;
	boolean allowJump = true;

	public ActionFear(AbstractAI ai, Attacker attacker, boolean canJump) {
		super(AbstractAI.ACTION_MOVE, ai);
		this.attacker = attacker;
		allowJump = canJump;
		normal = false;
	}

	@Override
	public void update(int delta) {
		float diffX;
		if (attacker == null || ai.getOwner() == null)
			end();
		if (((GameObject) attacker).getX() > ai.getOwner().getX()) {
			diffX = -ai.getOwner().getStat().getMoveSpeed() / 1000f * delta;
			ai.getOwner().setLeft(true);
		} else {
			diffX = ai.getOwner().getStat().getMoveSpeed() / 1000f * delta;
			ai.getOwner().setLeft(false);
		}

		if (!ai.getOwner().isCollision2(ai.getOwner().getX() + diffX,
				ai.getOwner().getY()))
			ai.getOwner().setX(ai.getOwner().getX() + diffX);
		else if (allowJump)
			ai.getOwner().setYacceleration(-ai.getOwner().getJumpStartSpeed());
		else
			end();

		if (ai.getOwner().distanceTo((GameObject) attacker) >= distanceToEnd
				&& ai.fear == 0) {
			end();
		}
	}

}
